(function ($) {
    String.prototype.getQuery = function (name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = this.substr(this.indexOf("\?") + 1).match(reg);
        if (r != null) return decodeURI(r[2]);
        return null;
    };

    var callBack = function (data, fn) {
        data = typeof(json) == "string" ? JSON.parse(data) : data;
        if (data.code == 1) {
            $.layer.msg(data.message);
            if (fn != null && typeof (eval(fn)) == "function") {
                fn(data);
            }
        } else if (data.code == 0) {
            $.layer.msg(data.message);
            if (fn != null && typeof (eval(fn)) == "function") {
                fn(data);
            }
        } else if (data.code == "-3") {
            $.layer.msg(data.message);
            window.location.href = "/Admin/user/toLoginPage";
        } else if (data.code == "-2") {
            $.layer.msg(data.message);
            if (window.parent.parent)
                window.parent.parent.location.href = loginPage;
            else if (window.parent)
                window.parent.location.href = loginPage;
            else window.location.href = loginPage;
        } else {
            $.layer.msg(data.message);
        }
    };

    var errorCallBack = function (data) {
        data = typeof(json) == "string" ? JSON.parse(data) : data;
        $.layer.msg(data.message);
    };


    $.extend({
        include: function (path, file) {
            var files = typeof file == "string" ? [file] : file;
            for (var i = 0; i < files.length; i++) {
                //var name = files[i].replace(/^s|s$/g, "");
                var name = files[i];
                var att = name.split('.');
                var ext = att[att.length - 1].toLowerCase();
                var isCSS = ext == "css";
                var tag = isCSS ? "link" : "script";
                var attr = isCSS ? " type='text/css' rel='stylesheet' " : " language='javascript' type='text/javascript' ";
                var link = (isCSS ? "href" : "src") + "='" + path + name + "'";
                if ($(tag + "[" + link + "]").length == 0)
                    document.write("<" + tag + attr + link + "></" + tag + ">");
            }
        },
        ajaxSubmitCallBack: function (data, fn) {
            callBack(data, fn);
        },
        $put: function (url, params, fn) {
            params["_method"] = "put";
            $.ajax({
                type: 'POST',
                url: url,
                data: params,
                dataType: "json",
                success: function (data) {
                    callBack(data, fn);
                },
                error: function (data) {
                    errorCallBack(data.responseJSON, fn);
                }
            });
        },
        $delete: function (url, params, fn) {
            params["_method"] = "delete";
            $.ajax({
                url: url,
                type: 'POST',
                dataType: 'json',
                data: params,
                success: function (data) {
                    callBack(data, fn);
                },
                error: function (data) {
                    errorCallBack(data.responseJSON, fn);
                }
            });
        },
        $get: function (url, params, fn, async) {
            $.ajax({
                url: url,
                type: 'get',
                cache: false,
                async: typeof(async) == "undefined" ? true : async,
                data: params,
                dataType: 'json',
                success: function (data) {
                    callBack(data, fn);
                },
                error: function (data) {
                    errorCallBack(data.responseJSON, fn);
                }
            });
        },
        $post: function (url, params, fn, async) {
            $.ajax({
                url: url,
                type: 'post',
                cache: false,
                async: typeof(async) == "undefined" ? true : async,
                data: params,
                dataType: 'json',
                success: function (data) {
                    callBack(data, fn);
                },
                error: function (data) {
                    errorCallBack(data.responseJSON, fn);
                }
            });
        },
        showPage: function (id, title, url) {
            if (url == "" || url == null) {
                return;
            }
            var breadcrumb = $(".breadcrumb");
            if (breadcrumb.length == 0) {
                breadcrumb = $(".breadcrumb", window.parent.document);
            }
            breadcrumb.find("li:last").text(title);

            var pageBody = $(".page-body");
            if (pageBody.length == 0) {
                pageBody = $(".page-body", window.parent.document);
            }
            var pageContent = $(".page-content");
            if (pageContent.length == 0) {
                pageContent = $(".page-content", window.parent.document);
            }
            pageBody.html('').html('<iframe src="' + url + '" scrolling="yes" frameborder="0"></iframe>');
            pageBody.find("iframe").css({
                "width": "100%",
                "height": pageContent.height() - 50
            });
        },
        picPreview: function (files, inputObj, targetObj) {
            if (files && files.length > 0) {
                // 获取目前上传的文件
                var file = files[0];// 文件大小校验的动作
                // 格式判断
                if (file.type != "image/png" && file.type != "image/PNG"
                    && file.type != "image/jpg" && file.type != "image/JPG"
                    && file.type != "image/jpeg" && file.type != "image/JPEG") {
                    $.layer.msg("图片只支持png/PNG/jpg/JPG/jpeg/JPEG格式");
                    $(inputObj).parents("form").find("button").attr({"disabled": "disabled"});
                    return false;
                }

                if (file.size > 1024 * 1024 * 20) {
                    alert('图片大小不能超过 2MB!');
                    return false;
                }
                // 获取 window 的 URL 工具
                var URL = window.URL || window.webkitURL;
                // 通过 file 生成目标 url
                var imgURL = URL.createObjectURL(file);
                //用attr将img的src属性改成获得的url
                if (!targetObj) {
                    targetObj = $("#picPreview");
                }
                targetObj.attr("src", imgURL);
                // 使用下面这句可以在内存中释放对此 url 的伺服，跑了之后那个 URL 就无效了
                // URL.revokeObjectURL(imgURL);
                $(inputObj).parents("form").find("button").removeAttr("disabled");
            }
        }
    });
})(jQuery);